package UMC.Web;

import UMC.Data.IJSON;
import UMC.Data.Utility;
import java.io.IOException;
import java.io.Writer;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;

public class UIView implements UMC.Data.IJSON {

    String _type;
    List<IJSON> items = new LinkedList<>();

    public UIView add(URI src, UIStyle style) {
        items.add(new WebMeta().put("src", src.toString()).put("style", style));
        return this;

    }

    public UIView(String cellName) {
        if (!cellName.startsWith("UMC_")) {
            throw new RuntimeException("cellName需要以“UMC_”为开头");
        }
        this._type = cellName;
    }

    public UIView() {

    }

    public UIView add(String key, URI src, UIStyle style) {
        items.add(new WebMeta().put("src", src.toString()).put("style", style).put("key", key));
        return this;

    }

    public UIView add(UIClick click, URI src, UIStyle style) {
        items.add(new WebMeta().put("src", src.toString()).put("style", style).put("click", click));
        return this;

    }

    public UIView add(String key, String text, UIStyle style) {
        items.add(new WebMeta().put("format", text).put("style", style).put("key", key));
        return this;
    }

    public UIView add(UIClick click, String text, UIStyle style) {
        items.add(new WebMeta().put("format", text).put("style", style).put("click", click));
        return this;
    }

    public UIView add(String text, UIStyle style) {
        items.add(new WebMeta().put("format", text).put("style", style));
        return this;

    }

    public URI Src;

    public String Key;


    @Override
    public void write(Writer writer) {

        if (Utility.isEmpty(_type)) {
            WebMeta webm = new WebMeta();
            if (this.Src != null) {
                webm.put("style", _style).put("items", this.items).put("src", this.Src.toString()).put("key", this.Key);//
            } else {
                webm.put("style", _style).put("items", this.items);//
            }

            UMC.Data.JSON.serialize(webm, writer);
        } else {
            try {
                writer.write("{");

                UMC.Data.JSON.serialize("key", writer);
                writer.write(":");
                UMC.Data.JSON.serialize(this._type, writer);


                writer.write(",");

                UMC.Data.JSON.serialize("data", writer);
                writer.write(":");
                WebMeta webm = new WebMeta();
                if (this.Src != null) {
                    webm.put("style", _style).put("items", this.items).put("src", this.Src.toString()).put("key", this.Key);//
                } else {
                    webm.put("style", _style).put("items", this.items);//
                }

                UMC.Data.JSON.serialize(webm, writer);


                writer.write("}");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


    private UIStyle _style = new UIStyle();


    public UIStyle style() {

        return _style;


    }


    @Override
    public void read(String key, Object value) {

    }
}